<template>
  <div class="side-menu">
    <div class="profile" :class="{ expend: expend && isLogin }">
      <div class="flex">
        <ProfileAvatar size="small" class="avatar" />
        <div
          v-if="!isLogin"
          class="right-content cursor"
          @click="$router.push({ name: 'login' })"
        >
          <div class="inner">
            <div class="login">Login <i class="cursor"></i></div>
            <div class="info">Login with your wallet or email</div>
          </div>
        </div>

        <div v-if="isLogin" class="metamask-profile right-content">
          <div class="inner">
            <div class="name" @click="expend = !expend">
              <div class="flex-1">{{ profile.username }}</div>
              <i class="cursor" :class="{ down: !expend, up: expend }"></i>
            </div>
            <div class="info metamask">with Metamask</div>
            <div
              class="address"
              v-clipboard:copy="$store.state.walletAddress"
              v-clipboard:success="() => $message.info('Copied')"
            >
              {{ formatedAddress }}
            </div>
          </div>
        </div>
      </div>
      <div v-if="isLogin" class="logout-wrapper">
        <div class="logout" @click="logout">Logout</div>
      </div>
    </div>

    <div class="menu">
      <MenuItem routerName="home"> <IconHome />Home </MenuItem>
      <MenuItem routerName="discover"> <IconDiscover />Discover </MenuItem>
      <MenuItem routerName="message"> <IconMessage />Message </MenuItem>
      <MenuItem routerName="posts"> <IconProfile />Profile </MenuItem>
      <MenuItem routerName="settings"> <IconSetting />Settings </MenuItem>
      <MenuItem routerName="invitationCode">
        <IconInvitation />invitationCode
      </MenuItem>
    </div>
  </div>
</template>

<script>
import ProfileAvatar from '@/components/common/ProfileAvatar.vue'
import IconHome from '@/components/icons/menu/Home.vue'
import IconDiscover from '@/components/icons/menu/Discover.vue'
import IconMessage from '@/components/icons/menu/Message.vue'
import IconProfile from '@/components/icons/menu/Profile.vue'
import IconSetting from '@/components/icons/menu/Setting.vue'
import IconInvitation from '@/components/icons/menu/Invitation.vue'
import MenuItem from './Item.vue'
import { EventBus, LOGOUT_EVENT } from '@/utils/eventBus.js'

export default {
  components: {
    MenuItem,
    ProfileAvatar,
    IconHome,
    IconDiscover,
    IconMessage,
    IconProfile,
    IconSetting,
    IconInvitation,
  },

  data() {
    return {
      expend: false,
    }
  },

  created() {
    if (this.$store.state.isLogin) {
      this.$store.dispatch('user/getPorfile')
    }
  },

  computed: {
    profile() {
      return this.$store.state.user.profile
    },
    isLogin() {
      return this.$store.state.isLogin
    },
    formatedAddress() {
      const walletAddress = this.$store.state.walletAddress
      return `${walletAddress.substr(0, 6)}....${walletAddress.substr(-4)}`
    },
  },

  methods: {
    logout() {
      this.$store.dispatch('logout')
      EventBus.$emit(LOGOUT_EVENT)
    },
  },
}
</script>

<style lang="less" scoped>
.side-menu {
  box-sizing: border-box;
  background: #1f1f1f;
  padding: 20px 0;
  border-radius: 6px;
  width: 240px;
  color: white;
  font-size: 16px;
}

.flex-1 {
  flex-grow: 1;
}

.cursor {
  cursor: pointer;
}

.profile {
  position: relative;
  padding: 10px;
  margin: 0 20px;
  border-bottom: 1px solid #2e2e2e;
  &.expend {
    background: #161616;
    border-bottom: 1px solid transparent;
  }

  .inner {
    padding-bottom: 10px;
  }

  .flex {
    display: flex;
  }

  .avatar {
    flex-shrink: 0;
  }

  .info {
    color: #898989;
    line-height: 14px;
    font-size: 12px;
    margin-top: 5px;
    &.metamask {
      padding-left: 17px;
      background: url("~@/assets/icons/common/metamask.svg") left center
        no-repeat;
      background-size: 13px;
    }
  }
  .address {
    line-height: 14px;
    font-size: 12px;
    color: #bda28d;
    background: url("~@/assets/icons/common/copy.svg") right center no-repeat;
    background-size: 13px auto;
    cursor: pointer;
    padding: 5px 0;
    &:hover {
      background-color: rgba(255, 255, 255, 0.1);
    }
  }

  .right-content {
    flex-grow: 1;
    padding-left: 8px;
    overflow: hidden;
    .name {
      user-select: none;
      cursor: pointer;
      height: 25px;
      display: flex;
      align-items: center;
      i {
        margin-left: auto;
      }
      .flex-1 {
        width: 120px;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
    }

    .login {
      display: flex;
      align-items: center;
      i {
        margin-left: auto;
      }
    }
  }

  i {
    transition: transform 0.3s;
    display: block;
    width: 10px;
    height: 10px;
    background: url("~@/assets/icons/common/arrow-right.svg") center center
      no-repeat;
    background-size: contain;
    &.down {
      transform: rotate(90deg);
    }
    &.up {
      transform: rotate(-90deg);
    }
  }
}

.logout-wrapper {
  height: 0px;
  overflow: hidden;
  transition: height 0.1s;

  .logout {
    padding-top: 10px;
    padding-bottom: 3px;
    line-height: 14px;
    color: #bda28d;
    font-size: 14px;
    border-top: 1px solid #2E2E2E;
    background: url("~@/assets/icons/common/logout.svg") right 10px no-repeat;
    background-size: 15px auto;
    cursor: pointer;
  }
}

.expend {
  .logout-wrapper {
    height: 28px;
  }
}

.menu {
  padding-top: 12px;
}
</style>